package com.example.music_mediaplayer.model;

import com.example.music_mediaplayer.tool.LogoutT;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by quan on 2018/1/18.
 * 歌词行
 * 包括该行歌词的时间，歌词内容
 */

public class LrcRow implements Comparable<LrcRow>{
    public final static String TAG = "LrcRow";
    public String strTime; //歌词行的时间
    public long time; //将歌词行时间转换为long型
    public String content ; //该行歌词的内容

    //无参构造方法
    public LrcRow(){}
    //带参构造方法
    public LrcRow(String strTime , long time ,String content){
        this.strTime = strTime;
        this.time = time;
        this.content = content;
        LogoutT.logout("strTime"+strTime+" time"+time+" content"+content);
    }

    @Override
    public String toString() {
        return "["+strTime+"]"+content;
    }

    /**
     * 读取歌词的每一行内容，转换为LrcRow，加入到集合中
     * @param standardLrcline
     * @return
     */
    public static List<LrcRow> createRows(String standardLrcline){
        try{
//            if (standardLrcline.indexOf("[")!=0||standardLrcline.indexOf("]")!=9){
//                LogoutT.logout("4555555555");
//                return null;
//            }
            List<LrcRow> listTimes = new ArrayList<LrcRow>();
            if (standardLrcline.indexOf("[")==0){
                if (standardLrcline.indexOf("]")==9||standardLrcline.indexOf("]")==10){
                    int lastIndexOfRightBracket = standardLrcline.lastIndexOf("]");
                    String content = standardLrcline.substring(lastIndexOfRightBracket+1,standardLrcline.length());
                    String times = standardLrcline.substring(0,lastIndexOfRightBracket+1).replace("[","-").replace("]", "-");
                    //通过“-”来拆分字符串
                    String arrTimes[] = times.split("-");

                    for(String temp : arrTimes){
                        if (temp.trim().length()==0){
                            continue;
                        }
                        LrcRow lrcRow = new LrcRow(temp,timeConvert(temp),content);
                        listTimes.add(lrcRow);
                    }
                }else {
                    return null;
                }
            }else {
                return null;
            }

//            int lastIndexOfRightBracket = standardLrcline.lastIndexOf("]");
//            String content = standardLrcline.substring(lastIndexOfRightBracket+1,standardLrcline.length());
//            String times = standardLrcline.substring(0,lastIndexOfRightBracket+1).replace("[","-").replace("]", "-");
//            //通过“-”来拆分字符串
//            String arrTimes[] = times.split("-");
//            List<LrcRow> listTimes = new ArrayList<LrcRow>();
//            for(String temp : arrTimes){
//                if (temp.trim().length()==0){
//                    continue;
//                }
//                LrcRow lrcRow = new LrcRow(temp,timeConvert(temp),content);
//                listTimes.add(lrcRow);
//            }
            return listTimes;
        }catch (Exception e){
            LogoutT.logout("createRows exception:" + e.getMessage());
            return null;
        }
    }

    /**
     * 将解析得到的表示时间的字符转化为Long型
     * @param timeString
     * @return
     */
    public static long timeConvert(String timeString){
        //将字符串XX:XX.XX转换为XX:XX:XX
        timeString = timeString.replace('.',':');
        //将字符串XX:XX:XX拆分
        String[] times = timeString.split(":");
        //mm:ss:SS
        return Integer.valueOf(times[0])*60*1000+   //分
                Integer.valueOf(times[1])*1000+   //秒
                Integer.valueOf(times[2]);   //毫秒

    }

    /**
     * 排序的时候，根据歌词的时间来排序
     * @param another
     * @return
     */
    public int compareTo(LrcRow another){
        return (int) (this.time - another.time);
    }

}
